home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Windows / SAMPLES / IMPORTER / IMP / COMIMP.CPP next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  8.4 KB  |  324 lines

  1. /* $Id: COMImp.cpp 1.6 1997/05/12 19:06:20 damien Exp $ */
  2.  
  3. // Copyright (c) 1990-1995 Ray Dream, Inc. All rights reserved.
  4.  
  5. // This file is an example of importer.
  6. // it's a simple importer of facets from a text file.
  7. //
  8. // the file is a set of surfaces designed as follow :
  9. //   number of points
  10. //   1st point, x coordinate, y coordinate and z coordinate,
  11. //   2nd point, id
  12. //   etc...
  13. //   last point
  14. //
  15. // for example, a square is :
  16. //  4
  17. //  0 0 0
  18. //  0 1 0
  19. //  1 1 0
  20. //  1 0 0
  21. //
  22.  
  23. #ifndef __COMIMP__
  24. #include "COMIMP.h"
  25. #endif
  26.  
  27. #ifndef __3DCOFAIL__
  28. #include "3DCoFail.h"
  29. #endif
  30.  
  31. #ifndef __I3DSHUTI__
  32. #include "I3DShUti.h"
  33. #endif
  34.  
  35. #ifndef __I3DSHTRE__
  36. #include "I3DShTre.h"
  37. #endif
  38.  
  39. #ifndef __I3DSHOBJ__
  40. #include "I3DShObj.h"
  41. #endif
  42.  
  43. #ifndef __I3DSHSCN__
  44. #include "I3DShScn.h"
  45. #endif
  46.  
  47. #ifndef __IMPDLL__
  48. #include "IMPDll.h"
  49. #endif
  50.  
  51. #include <stdio.h>
  52.  
  53. TEasyImporter::TEasyImporter() {
  54.     fCRef=0; // Reference counter
  55.     global_count_Obj++;
  56.     }
  57.  
  58. TEasyImporter::~TEasyImporter() {
  59.     global_count_Obj--;
  60.     }
  61.  
  62. //IUnknown Interface :
  63. HRESULT TEasyImporter::QueryInterface(THIS_ REFIID riid, LPVOID* ppvObj) {
  64.     *ppvObj=NULL;
  65.     
  66.     // The TDXFImporter knows the interfaces of the parent Objects
  67.   if (IsEqualIID(riid, IID_IUnknown))
  68.     *ppvObj=(LPVOID)this;
  69.   else if (IsEqualIID(riid, IID_I3DExImportFilter))
  70.     *ppvObj=(LPVOID)this;
  71.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  72.     *ppvObj=(LPVOID)this;
  73.   else if (IsEqualIID(riid, IID_I3DExtension))
  74.     *ppvObj=(LPVOID)this;
  75.     
  76.   // we must add reference if we return an interface
  77.   if (*ppvObj!=NULL) {
  78.     ((LPUNKNOWN)*ppvObj)->AddRef();
  79.     return NOERROR;
  80.     }
  81.   else {
  82.     return ResultFromScode(E_NOINTERFACE);
  83.     }
  84.     }
  85.  
  86. ULONG TEasyImporter::AddRef(THIS) {
  87.     return fCRef++;
  88.     }
  89.  
  90. ULONG TEasyImporter::Release(THIS) {
  91.     if (fCRef==1)
  92.         delete this; // last reference released, so object destroyed
  93.  
  94.     return fCRef--;
  95.     }
  96.  
  97. // I3DExtension methods :
  98. I3DExtension* TEasyImporter::Clone(THIS) {
  99.     TEasyImporter* theClone = new TEasyImporter;
  100.     if (theClone) {
  101.         theClone->AddRef();
  102.         theClone->fData=fData;
  103.         }
  104.     return theClone;
  105.     }
  106.  
  107. HRESULT TEasyImporter::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  108.     InitCoFailure(shellUtilities);
  109.     return NOERROR;
  110.     }
  111.  
  112. // I3DExDataExchanger methods :
  113. ExtensionDataMap* TEasyImporter::GetExtensionDataMap(THIS) {
  114.   return NULL;
  115.   }
  116.  
  117. void* TEasyImporter::GetExtensionDataBuffer(THIS) {
  118.   return &fData; // used by the shell to set the new parameters
  119.   }
  120.   
  121. HRESULT TEasyImporter::ExtensionDataChanged(THIS) {
  122.   return NOERROR;
  123.   }
  124.  
  125. HRESULT TEasyImporter::HandleEvent(THIS_ ULONG SourceID) {
  126.   return ResultFromScode(E_NOTIMPL);
  127.   }
  128.  
  129. short TEasyImporter::GetResID(THIS) {
  130.   return 128; // this is the view ID in the resource file.
  131.   }
  132.   
  133. void TEasyImporter::StartProgress(IShFileStream* afile)    {
  134.     fProgressMax=afile->GetFileSize();  
  135.     //if (fData.fIsSmooth) fProgressMax = fProgressMax*2;
  136.     gShellUtilities->ProgressStart("Importing ESY", fProgressMax);
  137.     }
  138.  
  139. // I3DExImportFilter methods :
  140.  
  141. BOOLEAN TEasyImporter::Prepare(char* /*fullPathName*/, I3DShScene* /*scene*/, I3DShTreeElement* /*fatherTree*/)    {
  142.     return TRUE;
  143.     }
  144.  
  145. BOOLEAN TEasyImporter::WantsOptionDialog()    {
  146.     return FALSE;
  147.     }
  148.  
  149. HRESULT TEasyImporter::DoImport(THIS_ char* fullPathName, I3DShScene* scene,
  150.                                                                 I3DShTreeElement* fatherTree) {
  151.     I3DShTreeElement* topTree;
  152.     FailInfo                    fi;
  153.     DECLAREVOLATILE(IShFileStream*, stream = NULL);
  154.  
  155.     if (TRY(fi)) {
  156.         gShellUtilities->CoCreateInstance(CLSID_StandardFileStream, NULL, CLSCTX_INPROC_SERVER,
  157.                                                                             IID_IShFileStream, (LPVOID*) &stream);
  158.         FailNIL(stream);
  159.         FailOSErr((short)stream->InitFileStream(fullPathName, kShStreamIn));
  160.  
  161.         StartProgress(stream);
  162.  
  163.         if (fatherTree == NULL)    {
  164.             I3DShGroup*     topGroup;
  165.             scene->CreateTreeRootIfNone();
  166.             topGroup = scene->GetTreeRoot();
  167.             topGroup->QueryInterface(IID_I3DShTreeElement, (LPVOID*) &topTree);
  168.             topGroup->Release();
  169.             }
  170.         else    {
  171.             gShellUtilities->CoCreateInstance(CLSID_StandardGroup, NULL, CLSCTX_INPROC_SERVER, IID_I3DShTreeElement, (LPVOID*) &topTree);
  172.             topTree->SetScene(scene);
  173.             fatherTree->InsertLast(topTree);
  174.             }
  175.         scene->CreateRenderingCameraIfNone(IDTYPE('c', 'o', 'n', 'i'), (fatherTree == NULL));    // Create a onical rendering camera if none, and a Distant light if we not importing in an existing scene
  176.         
  177.         DoReadEasyFile(stream, scene, topTree);
  178.                 
  179.         stream->Release();
  180.         stream = nil;
  181.         gShellUtilities->ProgressDone();
  182.         fi.Success();
  183.         }
  184.     else {
  185.         if (stream)    
  186.             stream->Release();
  187.         gShellUtilities->ProgressDone();
  188.         fi.ReSignal();
  189.         }
  190.     return NOERROR;
  191.     }
  192.  
  193. BOOLEAN TEasyImporter::WantsTopScene () {
  194.     return FALSE;
  195.     }
  196.  
  197. /* load an integer from a stream
  198. HRESULT ReadIntStream(IShFileStream* stream, int* res) {
  199.     char        c;
  200.     HRESULT    error;
  201.  
  202.     *res=0;
  203.     if ((error=stream->Get(&c)) != NOERROR) return error;
  204.     while ((c==' ') || (c==13) || (c==10) || (c==9))
  205.         if ((error=stream->Get(&c)) != NOERROR) return error;
  206.     if ((c<'0') || (c>'9')) return ERROR_INVALID_BLOCK;
  207.  
  208.     while ((c>='0') && (c<='9')) {
  209.         *res=*res*10+(c-'0');
  210.         if ((error=stream->Get(&c)) != NOERROR)
  211.             if (error==ERROR_HANDLE_EOF)
  212.                 c=0;
  213.             else
  214.                 return error;
  215.         }
  216.     return NOERROR;
  217.     }*/
  218.  
  219. // load a 3d vector from a stream
  220. HRESULT ReadStreamVector3D(IShFileStream* stream, VECTOR3D* res) {
  221.     Double    d;
  222.     HRESULT error;
  223.     
  224.     if ((error=stream->GetDouble(&d)) != NOERROR) return error;
  225.     (*res)[0]=d;
  226.     if ((error=stream->GetDouble(&d)) != NOERROR) return error;
  227.     (*res)[1]=d;
  228.     if ((error=stream->GetDouble(&d)) != NOERROR) return error;
  229.     (*res)[2]=d;
  230.     return NOERROR;
  231.     }
  232.  
  233. FACET3D* SetEasyFacet(VECTOR3D pA,VECTOR3D pB,VECTOR3D pC) {
  234.     //VECTOR3D vAB, vAC, vBC;
  235.     FACET3D* facet=new FACET3D;
  236.  
  237.     /*vAB=pB-pA;
  238.     vAC=pC-pA;
  239.     vBC=pC-pB;
  240.  
  241.     /*facet->fVertices[0].fNormal=vAB ^ vAC;
  242.     facet->fVertices[1].fNormal=vAB ^ vBC;
  243.     facet->fVertices[2].fNormal=vAC ^ vBC;*/
  244.         
  245.     facet->fVertices[0].fVertex[0]=pA[0];
  246.     facet->fVertices[0].fVertex[1]=pA[1];
  247.     facet->fVertices[0].fVertex[2]=pA[2];
  248.     facet->fVertices[1].fVertex[0]=pB[0];
  249.     facet->fVertices[1].fVertex[1]=pB[1];
  250.     facet->fVertices[1].fVertex[2]=pB[2];
  251.     facet->fVertices[2].fVertex[0]=pC[0];
  252.     facet->fVertices[2].fVertex[1]=pC[1];
  253.     facet->fVertices[2].fVertex[2]=pC[2];
  254.     
  255.     /*facet->fVertices[0].fNormal.Normalize();
  256.     facet->fVertices[1].fNormal.Normalize();
  257.     facet->fVertices[2].fNormal.Normalize();*/
  258.  
  259.     facet->fReserved=0;
  260.     return facet;
  261.     }
  262.  
  263.  
  264. void TEasyImporter::DoReadEasyFile(IShFileStream* stream, I3DShScene* scene, I3DShTreeElement* topTree)    {
  265.     long                            nbPoints;
  266.     int                                i;
  267.  
  268.     I3DShObject*            object = nil; VOLATILE(object);
  269.     VECTOR3D                    firstPoint;
  270.     VECTOR3D                    secondPoint;
  271.     VECTOR3D                    lastPoint;
  272.     I3DShPolygonList*    surface;
  273.  
  274.     I3DShInstance*            instance = nil; VOLATILE(instance);
  275.     I3DShTreeElement*        instanceTree;
  276.  
  277.     char                            objName[10];
  278.     short                            counter=0;
  279.  
  280.     FailOSErr(stream->GetLong(&nbPoints));
  281.  
  282.     while (nbPoints) {
  283.         gShellUtilities->CoCreateInstance(CLSID_StandardPolygonList, NULL, CLSCTX_INPROC_SERVER, IID_I3DShPolygonList, (LPVOID*) &surface);
  284.         surface->Init(FALSE /*no normals*/, FALSE /*no UV space*/);
  285.         
  286.         surface->PreAllocateFacets(nbPoints - 2/*nb of facets*/);
  287.     
  288.         FailOSErr(ReadStreamVector3D(stream, &firstPoint));
  289.         FailOSErr(ReadStreamVector3D(stream, &lastPoint));
  290.  
  291.         for (i=2; i<nbPoints; i++) {
  292.             secondPoint[0]=lastPoint[0];
  293.             secondPoint[1]=lastPoint[1];
  294.             secondPoint[2]=lastPoint[2];
  295.             FailOSErr(ReadStreamVector3D(stream, &lastPoint));
  296.             // add a facet
  297.             surface->AddFacet (SetEasyFacet(firstPoint, secondPoint, lastPoint));
  298.             }
  299.         surface->CalcNormals(30.0);    // 30 degrees
  300.         surface->QueryInterface(IID_I3DShObject, (LPVOID*) &object);
  301.         surface->Release();
  302.         COLOR3D    defColor;
  303.         defColor.Mode=0;
  304.         defColor.R=1.0;
  305.         defColor.G=0.0;
  306.         defColor.B=0.0;
  307.         defColor.A=1.0;
  308.         object->SetSimpleShading(&defColor, 1.0, 1.0, 0.0, 0.0);
  309.         sprintf(objName,"Easy %i",++counter);
  310.         object->SetName(objName);
  311.  
  312.         gShellUtilities->CoCreateInstance(CLSID_StandardInstance, NULL, CLSCTX_INPROC_SERVER, IID_I3DShInstance, (LPVOID*) &instance);
  313.         instance->Set3DObject(object);
  314.         instance->QueryInterface(IID_I3DShTreeElement, (LPVOID*) &instanceTree);
  315.         instanceTree->SetScene(scene);
  316.         instanceTree->CenterHotPointOnElement();
  317.         instance->Release();
  318.         topTree->InsertLast(instanceTree);
  319.  
  320.         FailOSErr(stream->GetLong(&nbPoints));
  321.         }
  322.     
  323.     }
  324.